const myUnit = require('./bin/unit/myUnit.js');
const path = require('path');
const staticResource = require('./static_resource_manage.js');
let source_code_server = function (app,prefix) {
    /*
    * 该函数处理source_code_db相关的服务
    * */

    app.post(prefix+'/queryEffect',function (req,res) {
        /*
        * 根据特效名查询数据库,并返回uri给前端
        * */

        let sql =
            'select htmlAddress' +
            ' '+'from source_code_table' +
            ' '+'where special_effect_name = \''+req.body.effectName+'\';';
        myUnit.myDB.sql.execute('source_code_db',sql,function (err,results) {
            try{
                if(results===[]){
                    res.status(404).send('file not find!');
                }else{
                    let prefix = '/effect';
                    let suffix = '../code/coffeCat/'+req.body.effectName;
                    staticResource.add(prefix,suffix);
                    let base = path.parse(myUnit.myCrypto.strDecode(results[0].htmlAddress)).base;
                    let data = prefix+'/'+ base;
                    res.send(data);
                }
            }catch(err){
                console.error('position :source_code_server.js->queryEffect');
                console.error(err);
            }

        });

    });



    app.post(prefix+'/queryEffectList',function (req,res) {
        /*
        * 发送特效名列表给前端
        * */
        try{
            let sql='select special_effect_name from source_code_table';

            myUnit.myDB.sql.execute('source_code_db',sql,function (err,results) {
                res.send(results);
            });
        }catch(err){
            console.error('position :source_code_server.js->queryEffectList');
            console.error(err);
        }

    });

};

module.exports = source_code_server;